////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef STRING_MANAGER_H
#define STRING_MANAGER_H

#include "String_IDs.h"		//General pack IDs
#include "ASprite.h"

//#ifdef IPHONEOS
//#include "IGPSendInfo.h"
//
//extern IGPSendInfo* g_pIgpSendInfo;
//extern char g_versionNumber[8];
//#endif

////////////////////////////////////////////////////////////////////////////////////////////////////



//#define NUM_PACKS		(1 + 13)	//General + 3 missions
//#define NUM_MASKS		((NUM_PACKS >> 5) + 1)

#define REFRESH_FULL	0x0001	//Forces a complete pack reload. Used when language changes
#define PACK_LOAD_FULL	0x0001	//Loads all pack data
#define PACK_LOAD_SKIP	0x0002	//Skips pack data, loading only the number of strings

////////////////////////////////////////////////////////////////////////////////////////////////////

#define PACK_GENERAL	0
#define PACK_MISSION	1

#define LANGUAGE_EN		0
#define LANGUAGE_FR		1
#define LANGUAGE_DE		2
#define LANGUAGE_IT		3
#define LANGUAGE_SP		4
#define LANGUAGE_KR		5
#define LANGUAGE_JP		6
#define LANGUAGE_SR		7


typedef enum
{
	FONT_MENU = 0,
	FONT_BROWN_GAME,
	FONT_WHITE_GAME,
	FONT_BLACK,
	FONT_GREEN_GAME,
	FONT_YELLOW,
	FONT_SHINE,
	FONT_BLACK_SMALL,
	FONT_BLACK_SMALL_SHINE,
	FONT_LOAD,
	FONT_SHREK_RESULT,
	FONT_RED,	
	NUM_OF_FONTS,
};

////////////////////////////////////////////////////////////////////////////////////////////////////

#define MAX_ESCAPE_DIGITS 6 //Maximum number or 'd' special character in a string.
#define ESCAPE_DIGITS_MAX_DIGITS 9	//Maximum number possible to digits in an escape digits 

////////////////////////////////////////////////////////////////////////////////////////////////////

static const int ASIAN_FONT_DEFAULT_LINE_SPACE = 8;

class CNDSFile;
class StringPack
{
public:
	StringPack();
	virtual ~StringPack();
	TChar*		GetString(int id);
	int			Load(CNDSFile * pFile, int dataSize, int flags);	//Loads a string pack from the given file
	void		Unload();
	int			GetNumStrings()	{	return m_nStrings;	}
	

private:
	int			m_nStrings;	//Number of strings in the pack
	uint16	*	m_pOffsets;	//Offset for each string in m_pData
	TChar	*	m_pData;	//String pack data
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class StringManager
{
public:
	//ASprite* GetFontSprite( int font );
	void   LoadFont(int fontID);
	void   LoadAllFont();
	void   UnLoadFont(int id);
	void   LoadString(int nLanguage);

public:
	StringManager();
	virtual ~StringManager();

	inline void	SetLanguage(int lang)
	{
		m_nLanguage	= lang;
//#ifdef IPHONEOS
//		if(g_pIgpSendInfo)
//			g_pIgpSendInfo->IGPSendGameInfo(m_nLanguage, g_versionNumber);
//#endif
	}
	inline int	GetLanguage()			{	return m_nLanguage;	}
	inline void	UsePack(int pack)
	{	
		m_pPackLoaded	|=	(1 << pack);	
	}

	void FreeAllFontRes();

	void FreeAllPack();

	inline void	FreePack(int pack)
	{	
		m_pPackLoaded	&=	~(1 << pack);
	}

	inline bool PackUsed(int pack)
	{
		return (m_pPackLoaded & (1 << pack)) ? true : false;
	}

	//Loads / frees unused string packs 
	int		RefreshPacks(int flags);

	//Retrieves the string having the given ID
	TChar*	GetString(int id);
	bool	TestString(int id);
	
    //////////////////////////////////////////////////////////////////////////////////////////////
	void SetFontLineSpace(int fontId, int lineSpace);
	int GetFontLineSpace(int fontId);
    //Here the color is not the real color value, just the index, just see the enum of COLOR_NAME
	void DrawString(int fontId, int stringId, int x, int y, int anchor, bool Upchar = false ,int color = COLOR_NULL, int alpha = 255,int rotate = 0, int x_scale = 100 , int y_scale = 100, bool WB = false,bool WH = false, int rotateAngle = 0);
    //////////////////////////////////////////////////////////////////////////////////////////////
    //Here the color is not the real color value, just the index, just see the enum of COLOR_NAME
	void DrawString(int fontId, TChar* s, int x, int y, int anchor, bool Upchar = false ,int color = COLOR_NULL, int alpha = 255,int rotate = 0, int x_scale = 100 , int y_scale = 100, bool WB = false,bool WH = false, int rotateAngle = 0);
    //////////////////////////////////////////////////////////////////////////////////////////////
    //Here the color is not the real color value, just the index, just see the enum of COLOR_NAME
	int DrawPage(int fontId, int strinfId, int x, int y, int anchor, int width, int color = COLOR_NULL, bool WB = false, bool unwrap = true);
	int DrawPage(int fontId, TChar* s, int x, int y, int start, int line_num, int character_num, int anchor, int width, int color = COLOR_NULL, bool WB = false,bool bDrawString = true, bool unwrap = true);
	void UpdateStringSize(int fontId, TChar* s);
	void UpdateStringSize(int fontId, int stringId);

	void SetEscapedDigitValue(int value, int index);
	void SetNumberString(int value);
	TChar* GetSpecialCharacterString(TChar c);

	int GetCurrentEscapeDigit() { return m_nCurrentlyEscapeDigit;}
	void SetCurrentEscapeDigit(int value) { m_nCurrentlyEscapeDigit = value;}

	//void mywcsup(wchar_t* dest,size_t len);

	bool IsAsianLanguage();

	void LoadAllString();
public:
	ASprite*		m_fonts[NUM_OF_FONTS];
	int				m_nFontSpriteID[NUM_OF_FONTS];

private:
	int				m_nLanguage;
	uint32			m_pPackLoaded;		//Text packs usage status
	StringPack		m_pPacks[8][NUM_PACKS];			//Text packs


	int m_digitValues[MAX_ESCAPE_DIGITS];
	int m_nCurrentlyEscapeDigit;
	TChar m_digitString[ESCAPE_DIGITS_MAX_DIGITS + 1];
private:
	void CreateAndSetKRFont(int fontID, int spriteID);
	void CreateAndSetJPFont(int fontID, int spriteID);
	void CreateAndSetSRFont(int fontID, int spriteID);
};

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

#endif